<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

Class CatManager{
  private $db = null;      
    
	function __construct($db)
  {
        $this->db = $db;        
  }
    function getAll()
    {
    	$this->db->startConnection();
    	$query = 'SELECT * FROM `category` ORDER BY `weight` ASC';
    	$objectArray = $this->db->get_data($query, 'cat');
    	$this->db->closeConnection();
    	return $objectArray;
    }
    
	function getCategory()
    {
      	$query = 'SELECT * FROM `category` ORDER BY `weight` ASC;';
    		$objectArray = $this->db->get_data($query, 'cat');
        return $objectArray;
    }
	function getAllName()
    {
    	$this->db->startConnection();
    	$query = 'SELECT name FROM category;';
    	$objectArray = $this->db->getMixedData($query);
    	$this->db->closeConnection();
    	return $objectArray;
    }
	
	
	
	function countCat()
    {
      	$query = 'SELECT COUNT(cid) FROM category WHERE status = 1;';
      	$rs22 = $this->db->dbQuery($query);
      	$rs22 = mysql_fetch_array($rs22);
        return $rs22[0];
    }	
	
	function delete($id)
    {
    	$this->db->startConnection();
    	$query ='UPDATE category
				SET status = 0
				WHERE cid = ' . var_export($id, true) . ';';
    	$query_book = 	'UPDATE entity
						SET status = 0
						WHERE nid IN (SELECT nid
						FROM book
						WHERE cid = '.var_export($id, true) . ');';
		$this->db->dbQuery($query);
		$this->db->dbQuery($query_book);
    }	
	
	public function getById($id) 
	{  	
		$query = "SELECT * FROM category WHERE cid = $id AND status = 1";
		$objectArray = $this->db->get_data($query, 'cat');		
		if (count($objectArray) == 0) {
			return FALSE;
		}
	  	else {
	  		return $objectArray[0];
	  	}
  	}
	function getCategoryLimit($id, $numRow)
    {
    	$b = $id*$numRow - $numRow;
    	$e = $numRow;
      	$query = 'SELECT * FROM category WHERE status = 1 ORDER BY cid LIMIT '.$b.','.$e.';';
    	$objectArray = $this->db->get_data($query, 'cat');
        return $objectArray;
    }
    
    function get($attr, $value)
    {
    	//$this->db->startConnection();
    	$query = 'SELECT * FROM category WHERE ' . $attr . ' = ' . var_export($value, true) . ' AND status = 1;';
    	$objectArray = $this->db->get_data($query, 'cat');
    	//$this->db->closeConnection();
    	if (empty($objectArray)) {
    		throw new Exception('Does not exist.');
    	}
    	return $objectArray[0];    	
    }
    
    /**
     * Create an instance object and save it into database.
     */
    
    function create($name, $weight, $status)
    {
    	$cat = new Category($this->db);
			$cat->init($name, $weight, $status);
    	$cat->save();
    	return $cat;    	
    }    
    
	public function getAllCatNames()
	{
		$query = "SELECT DISTINCT c.name AS `name`
				  FROM category AS c";		
		$result = $this->db->getMixedData($query);
		return $result;
	}
    
    public function setPointCat()
    {
        $query = "SELECT cid, ROUND(weight/(SELECT MAX(weight) FROM category), 2) AS point FROM category";
        $result = $this->db->getMixedData($query);       
        
		return $result;
    }
}
?>